package LeetCode.LeetCode2;

/**
 * 柱状图中最大的矩形
 * @author lumin
 * @date 2022/05/23 15:34
 **/
public class Num84 {
    public static int largestRectangleArea(int[] heights) {
        if(heights.length == 1){
            return heights[0];
        }
        int[] arr = new int[heights.length];
        arr[0] = heights[0];
        int left = 0;
        int max = 0;
        int min = heights[0];
        for (int i = 1; i < arr.length; i++) {
            if(heights[i] > (i - left + 1) * Math.min(heights[i - 1],heights[i])){
                arr[i] = heights[i];
                left = i;
                min = Math.min(heights[i],heights[i - 1]);
            }else{
                arr[i] = (i - left + 1) * Math.min(min,heights[i]);
            }
        }
        for (int i = 0; i < arr.length; i++) {
            max = Math.max(arr[i],max);
        }
        return max;
    }

    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5};
        System.out.println(largestRectangleArea(arr));
    }
}